// pages/accountDetail/index.js
import {
  UserAccountModel
} from '../../models/userAccount.js'
let userAccountModel = new UserAccountModel()
let start = 0
let end = 0
const data = {}
let isSwitch = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initDate: '2020-01-01 00:00:00',
    startYear: 2017,
    endYear: 2022,
    datas: [],
    count:0,
    totalAmount: 0,
    payment: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.search()
    
  },
  getPayment(e){
    console.log(e.detail.value)
    this.setData({
      payment: e.detail.value
    })
    data.payment = e.detail.value
  },
  listenerSwitch: function (e) {
    isSwitch = e.detail.value
    this.initData()
  },
  initData(){
    this.setData({
      datas: [],
      count: 0,
      totalAmount:0
    })
  },
  search(){
    
    data.start = start
    data.end = end
    let account
    if (isSwitch){
      account = userAccountModel.getWxpayAndAlipaySingleDetail(data)
    }else{
      account = userAccountModel.getWxpayAndAlipayTotalByDay(data)
    }
    
    account.then(
      res => {
        console.log(res)
        if(res.code == 1){
          let totalAmount = 0
          for (let i = 0; i < res.data.length; i++) {
            totalAmount += parseFloat(res.data[i]['amount'])
          }
          this.setData({
            datas: res.data,
            count: res.data.length,
            totalAmount: totalAmount.toFixed(2)
          })
        }
      }
    ).catch(e => {
      console.log(e)
    });
    

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sendTimeToFather(e) {
    var data = e.detail.formatTime
    var format = data.replace(/-/g, '/')
    start = Date.parse(format) / 1000

  },
  sendTimeToFather1(e) {
    var data = e.detail.formatTime
    var format = data.replace(/-/g, '/')
    end = Date.parse(format) / 1000
  },
})